package com.yun.platform.sys.controller;


import com.alibaba.fastjson.JSON;
import com.yun.platform.config.dto.RData;
import com.yun.platform.config.dto.Yun;
import com.yun.platform.excption.BizException;
import com.yun.platform.sys.entity.User;
import com.yun.platform.sys.service.UserService;
import com.yun.platform.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * author:laich  Date:2021/9/10
 *
 * @Desciprtion:
 */
@RestController
@RequestMapping("/nologin")
@Api(value = "非用户登录相关接口", tags = {""})
@Slf4j
public class NologinController {


    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private UserService userService;

    /**
     * 注册方法
     * @param name
     * @param phone
     * @param captcha
     * @return
     */
    @PostMapping("/reg")
    @ApiOperation("用户注册")
    public RData reg(@RequestParam("name")String name, @RequestParam("phone") String phone, @RequestParam("captcha")String captcha){

        if(userService.findByPhone(phone) == 1){
            throw  new BizException("您以注册，请直接登录");
        }
        //验证码是否合法
        ValueOperations<String,Object> operations = redisTemplate.opsForValue();
        Object captchaIn = operations.get("sms:"+phone);
        User user = new User();
        if(captcha.equals(String.valueOf("1234"))){
            user.setPhone(phone);
            user.setUserName(name);
            user.setCreateTime(new Date());
            Integer reg = userService.reg(user);

        }else {

            return RData.fail("验证失败，请重新发送验证码");
        }

        //合法，将个人信息，注册时间，添加到数据库
        //不合法，返回注册失败：验证码不正确
        return RData.success(user);
    }

    /**
     * 用户登录接口
     * @param phone
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String")
    })
    public RData login(@RequestParam("phone")String phone,@RequestParam("code")String code){
        String token = null;
        JWTUtil jwtUtil = new JWTUtil();
        Map<String,Object> objectMap =new HashMap<>();

        if("".equals(phone)){
            throw new BizException("手机不能为空");
        }
        long byPhone = userService.findByPhone(phone);
        if(byPhone == 0){
            throw new BizException("登录失败");
        }
        User login = userService.findLogin(phone);
        token = jwtUtil.createJWT(login.getId()+"", JSON.toJSONString(login), Yun.jwt_time, objectMap);
        login.setToken(token);
        RData.success("登录成功",login);
        return RData.success("登录成功",login);
    }

}
